<template>
  <div class="behavior-stats-container">
    <h2>用户行为分类统计</h2>

    <el-button type="primary" @click="$router.go(-1)" style="margin-bottom: 20px">
      返回
    </el-button>

    <el-alert v-if="behaviorStats.length === 0 && !loading" title="未查询到行为统计数据" type="warning" show-icon
      style="margin-bottom: 20px">
    </el-alert>

    <el-table :data="behaviorStats" style="width: 100%" border stripe v-loading="loading">
      <el-table-column prop="userId" label="用户ID" width="120" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名称" width="180"></el-table-column>

      <el-table-column prop="statDate" label="日期" width="180" align="center">
        <template #default="{ row }">
          {{ formatDate(row.statDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="description" label="行为类型" width="150" align="center"></el-table-column>
      <el-table-column prop="behaviorCount" label="行为次数" width="100" align="center"></el-table-column>
      <el-table-column prop="categoryDescription" label="涉及分类数" width="120" align="center"></el-table-column>
      <el-table-column label="行为新闻详情" align="center">
        <!-- 这里显示用户点击的新闻类型,但是因为库中没有添加新闻对应的id -->
      </el-table-column>
    </el-table>

    <!-- 添加分页组件 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handlePageChange" :current-page="currentPage"
      :page-sizes="[6, 10, 20, 50]" :page-size="pageSize" layout="prev, pager, next,total, sizes " :total="totalCount">
    </el-pagination>
  </div>
</template>

<script>
import axios from '@/utils/request';
import { InfoFilled } from '@element-plus/icons-vue';

export default {
  name: 'UserBehaviorCategoryStats',
  components: {
    InfoFilled
  },
  data() {
    return {
      behaviorStats: [],
      loading: false,
      currentPage: 1,
      pageSize: 6,
      totalCount: 0
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.loading = true;
      const url = '/userRetentionStats/category/stats';
      const params = {
        current: this.currentPage,
        size: this.pageSize
      };

      axios.get(url, { params }).then(response => {
        if (response.data && response.data.code === 200) {
          this.behaviorStats = response.data.data.records || [];
          this.totalCount = response.data.data.total;
          if (this.behaviorStats.length === 0) {
            this.$message.warning('未查询到行为统计数据');
          }
        } else {
          this.$message.error(response.data?.message || '获取行为统计失败');
        }
      }).catch(error => {
        console.error('获取行为统计失败:', error);
        this.$message.error('获取行为统计失败: ' + (error.response?.data?.message || error.message));
      }).finally(() => {
        this.loading = false;
      });
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.fetchData();
    },

    handlePageChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    formatDate(date) {
      if (!date) return '';
      try {
        const dateObj = typeof date === 'string' ? new Date(date) : date;
        const year = dateObj.getFullYear();
        const month = String(dateObj.getMonth() + 1).padStart(2, '0');
        const day = String(dateObj.getDate()).padStart(2, '0');
        const hours = String(dateObj.getHours()).padStart(2, '0');
        const minutes = String(dateObj.getMinutes()).padStart(2, '0');
        const seconds = String(dateObj.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      } catch (e) {
        return date;
      }
    }
  }
};
</script>

<style scoped>
.behavior-stats-container {
  padding: 20px;
}

.el-table {
  margin-top: 20px;
}
</style>